package br.com.dlm.simuladoitil.util;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import br.com.dlm.simuladoitil.R;
import br.com.dlm.simuladoitil.basicas.Questao;

public class ListaQuestoesAdapter extends BaseAdapter {

    private List<Questao> questoes;

    public ListaQuestoesAdapter(List<Questao> questoes) {
        this.questoes = questoes;
    }

    @Override
    public int getCount() {
        return questoes.size();
    }

    @Override
    public Object getItem(int position) {
        return questoes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Questao questao = questoes.get(position);

        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lista_questoes, null);
            holder = new ViewHolder();
            holder.textViewNumeroDaQuestao = (TextView) convertView.findViewById(R.id.textViewNumeroDaQuestao);
            holder.textViewFlagQuestaoRespondida = (TextView) convertView.findViewById(R.id.textViewFlagQuestaoRespondida);
            holder.textViewPergunta = (TextView) convertView.findViewById(R.id.textViewPergunta);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textViewNumeroDaQuestao.setText("Questão " + String.valueOf(questoes.indexOf(questao) + 1));
        holder.textViewFlagQuestaoRespondida.setText(questao.respondida == true ? "Respondida" : "");
        holder.textViewPergunta.setText(questao.pergunta);

        return convertView;
    }

    private class ViewHolder {
        TextView textViewNumeroDaQuestao;
        TextView textViewFlagQuestaoRespondida;
        TextView textViewPergunta;
    }
}
